/*************************************************************************
    > File Name: pub.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年06月08日 星期四 21时03分33秒
 ************************************************************************/

#include "PUB.h"

//通过文件名字获得文件类型
char *
get_mime_type(char *name) {
    char *dot;
    dot = strrchr(name, '.') ; //自右向左查找，"."字符;如不存在返回NULL
	/*
	*charset=iso-8859-1   西欧的编码，说明网站采用的编码是英文;
	*charset=gb2312       说明网站采用的编码是简体中文;
	*charset=utf-8        代表世界通用的语言编码,可以用到中文日文韩文等世界上所有语言编码上;
	*charset=euc-kr       说明网站采用的编码是韩文;
	*charset=big5         说明网站采用的编码是繁体中文;

	*以下是依据传递进来的文件名，使用后缀判断是何种文件类型,将对应的文件类型按照http定义的关键字发送回去
   */
	if (dot == (char*)0)
        return "text/plain; charset=utf-8";
    if (strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0)
        return "text/html; charset=utf-8";
    else if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0)
        return "image/jpeg";
    else if (strcmp(dot, ".gif") == 0)
        return "image/gif";
    else if (strcmp(dot, ".png") == 0)
        return "image/png";
    else if (strcmp(dot, ".css") == 0)
        return "text/css";
    else if (strcmp(dot, ".au") == 0)
        return "audio/basic";
    else if (strcmp(dot, ".wav") == 0)
        return "audio/wav";
    else if (strcmp(dot, ".avi") == 0)
        return "video/x-msvideo";
    else if (strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt") == 0)
        return "video/quicktime";
    else if (strcmp(dot, ".mpeg") == 0 || strcmp(dot, ".mpe") == 0)
        return "video/mpeg";
    else if (strcmp(dot, ".vrml") == 0 || strcmp(dot, ".wrl") == 0)
        return "model/vrml";
    else if (strcmp(dot, ".midi") == 0 || strcmp(dot, ".mid") == 0)
        return "audio/midi";
    else if (strcmp(dot, ".mp3") == 0)
        return "audio/mpeg";
    else if (strcmp(dot, ".ogg") == 0)
        return "application/ogg";
    else if (strcmp(dot, ".pac") == 0)
        return "application/x-ns-proxy-autoconfig";
	else
        return "text/plain; charset=utf-8";
}

void
strdecode(char *to, char *from){
	for(; *from != '\0'; ++to, ++from){
		if(from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2])){
			*to = hexit(from[1]) * 16 + hexit(from[2]);
			from += 2;
		}
		else{
			*to = *from;
		}
	}
	*to = '\0';
}

int 
hexit(char c){
	if(c >= '0' && c <= '9'){
		return c-'0';
	}
	if(c >= 'a' && c <= 'f'){
		return c - 'a' + 10;
	}
	if(c >= 'A' && c <= 'F'){
		return c - 'A' + 10;
	}

	return 0;
}
